package main

import (
    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/widget"
    "fyne.io/fyne/v2/driver/desktop"
    "fyne.io/fyne/v2/theme"
)

func main() {
    myApp := app.New()

    // 加载自定义图标
    icon, _ := fyne.LoadResourceFromPath("assets/icons/favicon.ico")
    myApp.SetIcon(icon)
    myWindow := myApp.NewWindow("Button Widget")
    addSystemTray(myApp,myWindow)

    label := widget.NewLabel("add icon")

    myWindow.SetContent(label)
    myWindow.Resize(fyne.NewSize(500, 500))
    //窗口居中
    myWindow.CenterOnScreen()
    myWindow.ShowAndRun()
}

// 系统托盘
func addSystemTray(a fyne.App, w fyne.Window) {
    if desk, ok := a.(desktop.App); ok {
        h := fyne.NewMenuItem("芹菜是一根葱", func() { w.Show() /* 显示窗口 */ })
        h.Icon = theme.HomeIcon() //图标
        m := fyne.NewMenu("MyApp",h)
        desk.SetSystemTrayMenu(m)
    }
    w.SetCloseIntercept(func() {
       w.Hide()
    })
}